//
//  TSAddVipViewController.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSAddVipViewController: TSBaseViewController {

    var tableView : UITableView!
    var accountField : UITextField!
    var passwordField : UITextField!
    var confirmField : UITextField!
    
    @objc func registerAction() {
        
        if checkCommitCondition() {
            
            IQKeyboardManager.shared().resignFirstResponder()
            
            CBToast.showToastAction()
            TSNetworkManager.request(TMSNetworkTools.register(username: accountField.text!, password: passwordField.text!, repassword: confirmField.text!), showHUD: true, success: { (data) in
                CBToast.hiddenToastAction()
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
                    CBToast.showToastAction(message: "注册成功")
                }
                self.dismiss(animated: true, completion: nil)
            }) { (error) in
                CBToast.hiddenToastAction()
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.delegate = self;
        
        creatCustomNavi()
        configViews()
    }
    
    func checkCommitCondition() -> Bool {
        
        guard accountField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入账号")
            return false
        }
        
        guard passwordField.text?.count != 0 else {
            CBToast.showToastAction(message: "请输入登录密码")
            return false
        }
        
        guard confirmField.text?.count != 0 else {
            CBToast.showToastAction(message: "请确认登录密码")
            return false
        }
        
        guard passwordField.text == confirmField.text else {
            CBToast.showToastAction(message: "新密码与确认密码输入不一致")
            return false
        }
        
        return true
    }
    
    @objc func popAction() {
        if navigationController != nil {
            navigationController?.popViewController(animated: true)
        } else {
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    deinit {
        self.navigationController?.delegate = nil
    }
}

extension TSAddVipViewController {
    
//    func configCustomNavi() {
//        
//        let naviView = UIView.init()
//        naviView.backgroundColor = WHITE_COLOR
//        view.addSubview(naviView)
//        naviView.snp.makeConstraints { (make) in
//            make.left.right.equalToSuperview()
//            make.top.equalTo(STATUSBAR_HEIGHT)
//            make.height.equalTo(NAVIGATIONBAR_HEIGHT)
//        }
//        
//        let backBtn = UIButton.init(type: .custom)
//        backBtn.setTitle("增加会员", for: .normal)
//        backBtn.setTitleColor(UIColor.hexadecimalColor(hexadecimal: "#282828"), for: .normal)
//        backBtn.setImage(UIImage(named: "general_navigation_back"), for: .normal)
//        backBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
//        backBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
//        backBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
//        backBtn.addTarget(self, action: #selector(popAction), for: .touchUpInside)
//        naviView.addSubview(backBtn)
//        backBtn.snp.makeConstraints { (make) in
//            make.centerY.equalToSuperview()
//            make.left.equalTo(12)
//            make.width.equalTo(100)
//        }
//    }
    
    func configViews() {
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: CGFloat(T_TABBARHEIGHT), width: SCREEN_WIDTH, height: SCREEN_HEIGHT - CGFloat(T_TABBARHEIGHT)), style: .plain)
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        self.view.addSubview(tableView)
        
        accountField = createInputField(placeholder: "请输入账号")
        tableView.addSubview(accountField)
        accountField.snp.makeConstraints { (make) in
            make.left.equalTo(90)
            make.top.equalToSuperview()
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 90 - 15, height: 50))
        }
        
        let accountLabel = UILabel.init()
        accountLabel.text = "账        号"
        accountLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#3B3B3B")
        accountLabel.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        tableView.addSubview(accountLabel)
        accountLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(accountField)
            make.left.equalTo(15)
            make.width.equalTo(65)
        }
        
        let line1 = drawLine()
        tableView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(accountField.snp.bottom)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*15, height: 1))
        }
                
        passwordField = createInputField(placeholder: "请输入登录密码")
        passwordField.isSecureTextEntry = true
        tableView.addSubview(passwordField)
        passwordField.snp.makeConstraints { (make) in
            make.left.equalTo(accountField)
            make.top.equalTo(line1.snp.bottom)
            make.size.equalTo(accountField)
        }
        
        let pwdLabel = UILabel.init()
        pwdLabel.text = "登录密码"
        pwdLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#3B3B3B")
        pwdLabel.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        tableView.addSubview(pwdLabel)
        pwdLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(passwordField)
            make.left.width.equalTo(accountLabel)
        }
        
        let line2 = drawLine()
        tableView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(passwordField.snp.bottom)
            make.size.equalTo(line1)
        }
                        
        confirmField = createInputField(placeholder: "请确认登录密码")
        confirmField.isSecureTextEntry = true
        tableView.addSubview(confirmField)
        confirmField.snp.makeConstraints { (make) in
            make.left.equalTo(accountField)
            make.top.equalTo(line2.snp.bottom)
            make.size.equalTo(accountField)
        }
        
        let confirmPwdLabel = UILabel.init()
        confirmPwdLabel.text = "确认密码"
        confirmPwdLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#3B3B3B")
        confirmPwdLabel.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        tableView.addSubview(confirmPwdLabel)
        confirmPwdLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(confirmField)
            make.left.width.equalTo(accountLabel)
        }
        
        let line3 = drawLine()
        tableView.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(confirmField.snp.bottom)
            make.size.equalTo(line1)
        }
                                
        
        let registerBtn = UIButton.init(type: .system)
        registerBtn.setTitle("注册", for: .normal)
        registerBtn.setTitleColor(WHITE_COLOR, for: .normal)
        registerBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        registerBtn.backgroundColor = BASE_RED_COLOR
        registerBtn.layer.cornerRadius = 24
        registerBtn.layer.masksToBounds = true
        registerBtn.addTarget(self, action: #selector(registerAction), for: .touchUpInside)
        tableView.addSubview(registerBtn)
        registerBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(line3.snp.bottom).offset(100)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
    }
    
    func creatCustomNavi() {
        
        let customStatus = UIView.init()
        customStatus.backgroundColor = BASE_RED_COLOR
        self.view.addSubview(customStatus)
        customStatus.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(STATUSBAR_HEIGHT)
        }
        
        let customNavi = UIView.init()
        customNavi.backgroundColor = BASE_RED_COLOR
        self.view.addSubview(customNavi)
        customNavi.snp.makeConstraints { (make) in
            make.left.right.equalTo(customStatus)
            make.top.equalTo(customStatus.snp.bottom)
            make.height.equalTo(NAVIGATIONBAR_HEIGHT)
        }
        
        let titleLabel = UILabel.init()
        titleLabel.text = "添加会员"
        titleLabel.textColor = WHITE_COLOR
        titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
        customNavi.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
    
    func createInputField(placeholder:String) -> UITextField {
        
        let attributeStr = NSMutableAttributedString.init(string: placeholder)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:BASE_GRAY_COLOR, NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15, weight: .medium)], range: NSMakeRange(0, placeholder.count))
        
        let textField = UITextField.init()
        textField.attributedPlaceholder = (attributeStr.copy() as! NSAttributedString)
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        return textField
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        return line
    }
}

extension TSAddVipViewController : UINavigationControllerDelegate {
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        let isShowHomePage = viewController.isKind(of: TSAddVipViewController.self)
        self.navigationController?.setNavigationBarHidden(isShowHomePage, animated: false)
    }
}
